﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class admin_cms_Checkquyen : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["timeout"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
               
                // load  checked với opiton

                loadgorup();
                loadadmin_module_page();
                LoadRoles();
            }


        }

    }
 


 
    protected void User_Insert(object sender, EventArgs e)
    {
        // xử lý nhóm người dùng và quyền
        string nhomnguoidung = "";
        //foreach (ListItem item1 in RadioButtonList1.Items)
        //{

        //    if (item1.Selected)
        //    {
        //        // Response.Write("Bạn đã chọn quyền: " + item1.Value + "<br />");
        //        nhomnguoidung = item1.Value;
        //    }
        //}
        nhomnguoidung = RadioButtonList1.SelectedItem.Value.ToString();
        foreach (ListItem item in CheckBoxList1.Items)
        {
           
            if (item.Selected)
               
            {
                DataTable dtrole = new DataTable();
                dtrole = DataAccess.ExeSQLToDataTable("select * from admin_roles where idgroup='"+nhomnguoidung+"' group by idmodule");
                int n=dtrole.Rows.Count;
                if (n==0)
                {
                    DataAccess.ExeNonSQL("Insert into admin_roles(Idmodule,idgroup) values('" + item.Value + "','" + nhomnguoidung + "')");
                }
                else
                {
                    for (int i = 0; i < n; i++)
                    {
                        if (item.Value == dtrole.Rows[i][1].ToString())
                        {

                        }
                        else
                        {
                            DataAccess.ExeNonSQL("Insert into admin_roles(Idmodule,idgroup) values('" + item.Value + "','" + nhomnguoidung + "')");
                        }
                    }
                }
              
               
            }
        }


        //
       


    }
    //xử lý check
    protected void loadgorup()
    {
        DataTable dtgroup = new DataTable();
        dtgroup = DataAccess.ExeSQLToDataTable("select * from admin_groupuser order by idgroup desc");
        RadioButtonList1.DataSource = dtgroup;
        RadioButtonList1.DataTextField = "namegroup";
        RadioButtonList1.DataValueField = "idgroup";
        RadioButtonList1.DataBind();
    }
    protected void loadadmin_module_page()
    {
        string n = "0";
        DataTable dtadmin_module_page = new DataTable();
        dtadmin_module_page = DataAccess.ExeSQLToDataTable("select * from admin_module_page where idcheck !='" + n + "'");
        CheckBoxList1.DataSource = dtadmin_module_page;
        CheckBoxList1.DataValueField = "idmodule";
        CheckBoxList1.DataTextField = "Name_Module";
        CheckBoxList1.DataBind();
    }

    protected void bt_click(object sender, EventArgs e)
    {
        string nhomnguoidung = "";
        foreach (ListItem item1 in RadioButtonList1.Items)
        {

            if (item1.Selected)
            {
                // Response.Write("Bạn đã chọn quyền: " + item1.Value + "<br />");
                nhomnguoidung = item1.Value;
            }
        }
        foreach (ListItem item in CheckBoxList1.Items)
        {

            if (item.Selected)
            {
                Response.Write("Bạn đã chọn modul: " + nhomnguoidung + "<br />");
                Response.Write("Bạn đã chọn modul: " + item.Value + "<br />");
                DataAccess.ExeNonSQL("Insert into admin_roles(Idmodule,idgroup) values('" + item.Value + "','" + nhomnguoidung + "')");
            }
        }
    }
    protected void LoadRoles()
    {
        string sql = "select admin_roles.idgroup,admin_groupuser.idgroup,admin_groupuser.namegroup from admin_roles, admin_groupuser where admin_roles.idgroup=admin_groupuser.idgroup";
        DataTable dt = new DataTable();
        dt = DataAccess.ExeSQLToDataTable("sql");
        if (dt.Rows.Count>0)
        {
            GrvGroupUser.DataSource = dt;
            GrvGroupUser.DataBind();
        }
    }
}
